﻿
function MacBox(id,pNode){
	this.id = id;
	this.onChange=new Function();
	this.onEnterKey=new Function();
	this.disabled=false;
	MacBox.all[id]=this;
	if(pNode)
	{
		if(typeof pNode=="string")
		{
			pNode=document.getElementById(pNode);
		}
		pNode.innerHTML=this.toString();
	}
}
MacBox.all={};
MacBox.EnabledClassName="macInput oneInput";//启用时样式
MacBox.DisabledClassName="macInput oneInputDisabled";// 禁用时样式
MacBox.prototype={
/**
* 激活MAC框
* @param {number} index 1-6　指定激活的位置
*/
focus:function(index){
	if(!index)
		index=1;
	document.getElementById(this.id+"_"+index).focus();
},
setEnable:function(bEnable){
	var b=!bEnable;
	this.disabled=!bEnable;
	var boxes=document.getElementById(this.id).getElementsByTagName("input");
	for(var i=0;i<boxes.length;i++)
	{
		boxes.readOnly=b;
	}
	document.getElementById(this.id).className=bEnable?MacBox.EnabledClassName:MacBox.DisabledClassName
},
toString:function(){
return '<span  id="'+this.id+'" class="' + MacBox.EnabledClassName + '">\
<input onkeypress="MacBox.evt.keypress(this,event)" onkeydown="MacBox.evt.keydown(this,event)" onfocus="MacBox.evt.focus(this,event)" onpaste="MacBox.evt.change(this,event);" oninput="MacBox.evt.change(this,event);" onchange="MacBox.evt.change(this,event);"  type="text" size=2  id="'+this.id+'_1" maxlength=2\
/>:<input onkeypress="MacBox.evt.keypress(this,event)" onkeydown="MacBox.evt.keydown(this,event)" onfocus="MacBox.evt.focus(this,event)" onpaste="MacBox.evt.change(this,event);" oninput="MacBox.evt.change(this,event);" onchange="MacBox.evt.change(this,event);"  type="text" size=2  id="'+this.id+'_2" maxlength=2\
/>:<input onkeypress="MacBox.evt.keypress(this,event)" onkeydown="MacBox.evt.keydown(this,event)" onfocus="MacBox.evt.focus(this,event)" onpaste="MacBox.evt.change(this,event);" oninput="MacBox.evt.change(this,event);" onchange="MacBox.evt.change(this,event);"  type="text" size=2  id="'+this.id+'_3" maxlength=2\
/>:<input onkeypress="MacBox.evt.keypress(this,event)" onkeydown="MacBox.evt.keydown(this,event)" onfocus="MacBox.evt.focus(this,event)" onpaste="MacBox.evt.change(this,event);" oninput="MacBox.evt.change(this,event);" onchange="MacBox.evt.change(this,event);"  type="text" size=2  id="'+this.id+'_4" maxlength=2\
/>:<input onkeypress="MacBox.evt.keypress(this,event)" onkeydown="MacBox.evt.keydown(this,event)" onfocus="MacBox.evt.focus(this,event)" onpaste="MacBox.evt.change(this,event);" oninput="MacBox.evt.change(this,event);" onchange="MacBox.evt.change(this,event);"  type="text" size=2  id="'+this.id+'_5" maxlength=2\
/>:<input onkeypress="MacBox.evt.keypress(this,event)" onkeydown="MacBox.evt.keydown(this,event)" onfocus="MacBox.evt.focus(this,event)" onpaste="MacBox.evt.change(this,event);" oninput="MacBox.evt.change(this,event);" onchange="MacBox.evt.change(this,event);"  type="text" size=2  id="'+this.id+'_6" maxlength=2/>\
</span>';
},
getValue:function(){
	var mac=[
	document.getElementById(this.id+"_1").value,
	document.getElementById(this.id+"_2").value,
	document.getElementById(this.id+"_3").value,
	document.getElementById(this.id+"_4").value,
	document.getElementById(this.id+"_5").value,
	document.getElementById(this.id+"_6").value
	];
	return mac.join(":");
},
setValue:function(mac){
	mac=mac.replace(/[^[a-f0-9]:]/g,"");
	if(mac=="")
	{
		mac=":::::"
	}
	mac=mac.split(":");
	document.getElementById(this.id+"_1").value = mac[0];
	document.getElementById(this.id+"_2").value = mac[1];
	document.getElementById(this.id+"_3").value = mac[2];
	document.getElementById(this.id+"_4").value = mac[3];
	document.getElementById(this.id+"_5").value = mac[4];
	document.getElementById(this.id+"_6").value = mac[5];
	}
}
MacBox.evt = {
    focus: function(obj, evt) {
        obj.select();
    },
    change: function(obj, evt) {
        var v = obj.value;
        var reghex = new RegExp("(^[a-f0-9]{1,2}$)|(^[A-F0-9]{1,2}$)");
        if (reghex.exec(v) !== null) {
            if (v != obj.value)
                obj.value = v;
        }
        else {
            obj.value = "";
        }
        MacBox.all[obj.id.replace(/_\d$/, "")].onChange();
    },
    keypress: function(obj, evt) {
        var key = evt.charCode || evt.keyCode;
        var pos = MacBox.evt.getSelection(obj);
        var value = obj.value;
        var c = String.fromCharCode(key);
        if ((key >= 48 && key <= 57) || (key >= 65 && key <= 70) || (key >= 97 && key <= 102)) {
            value = "" + value.substring(0, pos.start) + c + value.substring(pos.end, value.length);
            var reghex = new RegExp("(^[a-f0-9]{1,2}$)|(^[A-F0-9]{1,2}$)");
            if (reghex.exec(value) !== null) {
                var id = obj.id;
                /(.*)_(\d)$/.test(id);
                var index = RegExp.$2;
                MAC_id = RegExp.$1;
                if (value.length > 1) {
                    if (parseInt(index) < 6) {
                        id = id.replace(/(\d)$/, parseInt(index) + 1);
                        setTimeout("document.getElementById('" + id + "').focus();" + "document.getElementById('" + id + "').select();", 200);
                    }
                }
                setTimeout("MacBox.all['" + MAC_id + "'].onChange()", 0);
                return true;
            }
            else {
                if (evt.preventDefault)
                    evt.preventDefault();
                evt.returnValue = false;
                return false;
            }
        }
        else {
            if (evt.preventDefault)
                evt.preventDefault();
            evt.returnValue = false;
        }
    },
    keydown: function(obj, evt) {
        var key = evt.charCode || evt.keyCode;
        var pos = MacBox.evt.getSelection(obj);
        var value = obj.value;
        var c = String.fromCharCode(key);
        var id = obj.id;
        /^(.*)_(\d)$/.test(id);
        var index = RegExp.$2;
        var MAC_id = RegExp.$1;
        var reghexUpcase = new RegExp("(^[A-F0-9]{1,2}$)");
        var isUpcase; //默认情况下，数字属于大写
        if (reghexUpcase.exec(value) !== null)
            isUpcase = true;
        else
            isUpcase = false;
        switch (key) {
            case 13: //回车
                MacBox.all[MAC_id].onEnterKey();
                break;
            case 110: //.小键盘
            case 190: //.主键盘
                if (index < 6) {
                    if (value.length > 0) {
                        id = id.replace(/(\d)$/, parseInt(index) + 1);
                        document.getElementById(id).focus();
                        document.getElementById(id).select();
                    }
                }
                break;
            case 38: //up自加
                //value=!isNaN(parseInt(value))?parseInt(value):"";
                if (value == "")
                    value = 0;
                if (parseInt(value, 16) < parseInt("FF", 16)) {
                    if (isUpcase) {
                        obj.value = (parseInt(value, 16) + 1).toString(16).toUpperCase();
                    } else {
                        obj.value = (parseInt(value, 16) + 1).toString(16);
                    }
                }
                else
                    obj.value = 0;
                break;
            case 40: //down自减
                //value=!isNaN(parseInt(value))?parseInt(value):"";
                if (value == "")
                    value = "FF";
                if (parseInt(value, 16) > 0) {
                    if (isUpcase) {
                        obj.value = (parseInt(value, 16) - 1).toString(16).toUpperCase();
                    } else {
                        obj.value = (parseInt(value, 16) - 1).toString(16);
                    }
                };
                break;
            case 8: //backspace
                if (pos.start > 0)
                    return;
            case 37: //left
                if (pos.end == 0 && index > 1) {
                    id = id.replace(/(\d)$/, parseInt(index) - 1);
                    document.getElementById(id).focus();
                    document.getElementById(id).select();
                }
                break;
            case 39: //right
                if (pos.start == value.length && index < 6) {
                    id = id.replace(/(\d)$/, parseInt(index) + 1);
                    document.getElementById(id).focus();
                    document.getElementById(id).select();
                }
                break;
        }
    },
    //获取选区位置
    getSelection: function(oInput) {
        var T = this;
        if (oInput.createTextRange) {
            var s = document.selection.createRange().duplicate();
            s.moveStart("character", -oInput.value.length);
            var p1 = s.text.length;
            var s = document.selection.createRange().duplicate();
            s.moveEnd("character", oInput.value.length);
            var p2 = oInput.value.lastIndexOf(s.text);
            if (s.text == "") p2 = oInput.value.length;
            return { start: p2, end: p1 };
        } else {
            return { start: oInput.selectionStart, end: oInput.selectionEnd };
        }
    }
}
